package nl.nickthissen.remindme.activities;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.activeandroid.query.Select;

import java.util.ArrayList;

import nl.nickthissen.remindme.R;
import nl.nickthissen.remindme.adapters.ReminderItemAdapter;
import nl.nickthissen.remindme.data.Category;
import nl.nickthissen.remindme.data.CategoryManager;
import nl.nickthissen.remindme.data.ReminderItem;
import nl.nickthissen.remindme.views.SwipeDismissListViewTouchListener;

public class MainActivity extends ListActivity
{
    private static final int ACTION_EDIT_ITEM = 1;
    private static final int ACTION_EDIT_CAT = 2;

    private ArrayList<ReminderItem> items;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        CategoryManager.initialize();

        final ListView lv = this.getListView();
        lv.setOnTouchListener(new SwipeDismissListViewTouchListener(lv, new SwipeDismissListViewTouchListener.DismissCallbacks()
        {
            @Override
            public boolean canDismiss(int position)
            {
                return true;
            }

            @Override
            public void onDismiss(ListView listView, int[] reverseSortedPositions)
            {
                for (int i : reverseSortedPositions)
                {
                    ReminderItem item = items.get(i);
                    if (item != null) deleteItem(item);
                    loadItems();
                }
            }
        }));
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                ReminderItem item = items.get(position);
                editItem(item.getId());
            }
        });
        this.loadItems();
    }

    private void loadItems()
    {
        items = new Select().from(ReminderItem.class).orderBy("ReminderDate").execute();
        this.setItems();
    }

    private void setItems()
    {
        ReminderItemAdapter adapter = new ReminderItemAdapter(this, R.layout.item_row, items);
        this.setListAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.action_newitem:
                editItem(0);
                return true;
            case R.id.action_newcat:
                editCategory(0);
                return true;

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        this.loadItems();
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void editItem(long id)
    {

        Intent i = EditReminderItemActivity.getIntent(this, id);
        this.startActivityForResult(i, ACTION_EDIT_ITEM);
    }

    private void editCategory(long id)
    {
        Intent i = EditCategoryActivity.getIntent(this, id);
        this.startActivityForResult(i, ACTION_EDIT_CAT);
    }

    private void deleteItem(ReminderItem item)
    {
        deleteItem(item, false);
    }

    private void deleteItem(ReminderItem item, boolean reload)
    {
        item.delete();
        if (reload) this.loadItems();
    }

    public void onItemClicked(ReminderItem item)
    {
        this.editItem(item.getId());
    }
    
}
